package com.zhoufeng.udp.service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;

import com.zhoufeng.common.Common;
import com.zjhbkj.xinfen.commom.Global;
import com.zjhbkj.xinfen.db.DBMgr;
import com.zjhbkj.xinfen.model.MsgInfo;
import com.zjhbkj.xinfen.model.RcvComsModel;
import com.zjhbkj.xinfen.model.SendComsModel;
import com.zjhbkj.xinfen.util.CommandUtil;

import de.greenrobot.event.EventBus;

public class UDPServerRun implements Runnable {
	private DatagramSocket mReceiveSocket;
	private DatagramSocket mSendSocket;
	private byte[] msg;
	private boolean onGoinglistner;
	
	public UDPServerRun() {
		byte[] arrayOfByte = new byte[Common.COMMAND_LENGTH];
		this.msg = arrayOfByte;
		this.onGoinglistner = true;
		init();
	}

	private void init() {
		try {
			if (this.mReceiveSocket == null) {
				DatagramSocket localDatagramSocket1 = new DatagramSocket(null);
				this.mReceiveSocket = localDatagramSocket1;
				this.mReceiveSocket.setReuseAddress(true);
				DatagramSocket localDatagramSocket2 = this.mReceiveSocket;
				int i = Global.DEVICE_PORT;
				InetSocketAddress localInetSocketAddress = new InetSocketAddress(i);
				localDatagramSocket2.bind(localInetSocketAddress);
			}
			DatagramSocket localDatagramSocket3 = new DatagramSocket();
			this.mSendSocket = localDatagramSocket3;
			return;
		} catch (SocketException localSocketException) {
			String str = localSocketException.toString();
			// int j = Log.d("aaa", str);
			localSocketException.printStackTrace();
		}
	}

	public void closeConnection() {
		if (this.mReceiveSocket != null) {
			this.mReceiveSocket.close();
			this.mReceiveSocket = null;
		}
		if (this.mSendSocket == null)
			return;
		this.mSendSocket.close();
		this.mSendSocket = null;
	}

	public void run() {
		byte[] arrayOfByte1 = this.msg;
		int i = this.msg.length;
		DatagramPacket localDatagramPacket = new DatagramPacket(arrayOfByte1, i);
		while (true) {
			if (!this.onGoinglistner)
				return;
			try {
				this.mReceiveSocket.receive(localDatagramPacket);
				byte[] arrayOfByte2 = localDatagramPacket.getData();

				System.out.println("接收成功！！");
				System.out.println(arrayOfByte2.length);

				RcvComsModel obj = (RcvComsModel) ByteToObject(arrayOfByte2);
				System.out.println(obj.getCommand1());
				System.out.println(obj);
				
				String send = "接收成功";
				byte[] bsend = send.getBytes();
				InetAddress addr = localDatagramPacket.getAddress();
				int port = localDatagramPacket.getPort();
				
				
				
				DatagramPacket sendPacket = new DatagramPacket(bsend, bsend.length, addr, port);
				
				this.mReceiveSocket.send(sendPacket);
				
				// boolean bool =
				// localRcvComsModel.receiveCommand(arrayOfByte2);
				// int j = this.msg.length;
				// localDatagramPacket.setLength(j);
				// if (bool)
				// {
				// DBMgr.saveModel(localRcvComsModel);
				// EventBus.getDefault().post(localRcvComsModel);
				// send(localDatagramPacket);
				// }
			} catch (Exception localIOException) {
				localIOException.printStackTrace();
			}
		}
	}

	public void send(DatagramPacket paramDatagramPacket) {
		SendComsModel localSendComsModel = (SendComsModel) DBMgr.getHistoryData(SendComsModel.class, "EA");
		if (localSendComsModel == null)
			return;
		byte[] arrayOfByte = CommandUtil.getCommand(localSendComsModel.toString());
		int i = arrayOfByte.length;
		InetAddress localInetAddress1 = paramDatagramPacket.getAddress();
		int j = paramDatagramPacket.getPort();
		DatagramPacket localDatagramPacket = new DatagramPacket(arrayOfByte, i, localInetAddress1, j);
		try {
			this.mSendSocket.send(localDatagramPacket);
			EventBus localEventBus1 = EventBus.getDefault();
			StringBuilder localStringBuilder1 = new StringBuilder("消息发送成功");
			InetAddress localInetAddress2 = paramDatagramPacket.getAddress();
			StringBuilder localStringBuilder2 = localStringBuilder1.append(localInetAddress2).append(":");
			int k = paramDatagramPacket.getPort();
			String str1 = new Integer(k).toString();
			localEventBus1.post(str1);
			return;
		} catch (IOException localIOException) {
			localIOException.printStackTrace();
			EventBus localEventBus2 = EventBus.getDefault();
			StringBuilder localStringBuilder3 = new StringBuilder("消息发送失败.");
			String str2 = localIOException.toString();
			String str3 = str2;
			localEventBus2.post(str3);
		}
	}

	public void stopAcceptMessage() {
		this.onGoinglistner = false;
	}

	public static abstract interface DataRecvListener {
		public abstract void onRecv(MsgInfo paramMsgInfo);
	}

	public static Object ByteToObject(byte[] bytes) throws IOException, ClassNotFoundException {
		ByteArrayInputStream bs = new ByteArrayInputStream(bytes);
		ObjectInputStream os = new ObjectInputStream(bs);
		Object m = os.readObject();
		return m;

	}
	
	public static void main(String[] args) {
		new  UDPServerRun().run();
	}
	

}